---
category: DOM
created: '2020-04-19'
title: Replace an element
updated: '2021-10-13'
---

**JavaScript version**

```js replace.js
const replace = (ele, newEle) => ele.parentNode.replaceChild(newEle, ele);
```

**TypeScript version**

```ts replace.ts
const replace = (ele: Element, newEle: Element): Element | null =>
    ele.parentNode ? ele.parentNode.replaceChild(newEle, ele) : null;
```
